package com.kamistoat.meimeimall.meimeimallcart.web;

import com.alibaba.fastjson.JSON;
import com.kamistoat.meimeimall.common.To.orderTo.OrderItemTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallcart.dubbo.CartDubboInterfaceImpl;
import com.kamistoat.meimeimall.meimeimallcart.service.CartService;
import com.kamistoat.meimeimall.meimeimallcart.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="用户购物车页面")
@RestController
@RequestMapping("web")
public class CartPageController {

    protected Logger logger = LoggerFactory.getLogger(CartPageController.class);

    @Autowired
    CartService cartService;

    @Autowired
    CartDubboInterfaceImpl cartDubboInterface;

    /**
     * 获取用户购物车详细信息
     */
    @ApiOperation("获取用户缓存购物车详细信息")
    @LoginRequired
    @GetMapping("/get_cart_items")
    @DubboAndHTTPExceptionCatch
    public R cartListPage() {
        CartVo cartVo = cartService.getCart();
        return R.ok().put("cartVo", cartVo);
    }

    /**
     * 勾选/取消勾选 购物车中的商品
     *
     * @param skuId
     * @param check
     * @return
     */
    @ApiOperation("勾选/取消勾选 缓存购物车中的商品")
    @PostMapping("/cart_item_check")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R checkItem(@RequestParam Long skuId, @RequestParam Integer check) {
        cartService.checkItem(skuId, check);
        return R.ok();
    }

    /**
     * 改变购物车中商品的数量
     *
     * @param skuId
     * @param num
     * @return
     */
    @ApiOperation("改变缓存购物车中的商品数量")
    @PostMapping("/cart_item_count")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R countItem(@RequestParam Long skuId, @RequestParam Integer num) {
        cartService.countItem(skuId, num);
        return R.ok();
    }

    /**
     * 删除某个商品
     *
     * @param skuId
     * @return
     */
    @ApiOperation("删除缓存购物车中的某个商品")
    @DeleteMapping("/deleteItem")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R deleteItem(@RequestParam Long skuId) {
        cartService.deleteItem(skuId);
        return R.ok();
    }

    /**
     * 获取用户购物车所有被选中的商品项
     */
    @ApiOperation("获取缓存购物车中所有被选中的商品项")
    @GetMapping("/get_cart_items_checked")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R getOrderCartChecked() {
        List<OrderItemTo> orderItemToList = cartService.getOrderCartChecked();
        return R.ok().put("orderItemToList", JSON.toJSONString(orderItemToList));
    }

}
